#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void SeqListInit(SeqList* ps)
{
	ps->capacity = MIN_CA;
	ps->size = 0;
	ps->a = (SLDateType*)malloc(sizeof(SLDateType)*MIN_CA);
	if (ps->a == NULL)
	{
		perror("malloc failed");
		return;
	}
}

void SeqListDestroy(SeqList* ps)
{
	free(ps->a);
	ps->size = 0;
	ps->capacity = 0;
	ps->a = NULL;
}

void SeqListCheck(SeqList* ps)
{
	if (ps->size == ps->capacity)
	{
		SLDateType* tmp = (SLDateType*)realloc(ps->a, ps->capacity * 2 * sizeof(SLDateType));
		if (tmp == NULL)
		{
			perror("realloc failed");
			return;
		}
		ps->a = tmp;
		ps->capacity = ps->capacity * 2;
	}
}

void SeqListPrint(SeqList* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
		printf("%d ", ps->a[i]);
}

int SeqListFind(SeqList* ps, SLDateType x) 
{
	int pos = 0;
	while (ps->a[pos] != x && pos < ps->size)
	{
		pos++;
	}
	if (pos != ps->size)return pos;
	return -1;
}

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	SeqListCheck(ps);
	ps->a[ps->size++] = x;
}

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	SeqListCheck(ps);
	int end = ps->size-1;
	while (end >= 0)
	{
		ps->a[end+1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}

void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SeqListCheck(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}


void SeqListPopFront(SeqList* ps)
{
	if (ps->size == 0)return;
	int begin =0;
	while (begin < ps->size-1)
	{
		ps->a[begin] = ps->a[begin + 1];
		++begin;
	}
	ps->size--;	
}



void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	if (ps->size == 0)return;
	ps->size--;
}

void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	while (pos < ps->size-1)
	{
		ps->a[pos] = ps->a[pos + 1];
		pos++;
	}
	ps->size--;
}